//
//  NotificationCenterTestViewController.swift
//  iOS-RxSwift-Tutorials_Example
//
//  Created by mengru.tian on 2020/9/24.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class MyObserver: NSObject {
     
    var name:String = ""
     
    init(name:String){
        super.init()
         
        self.name = name
         
        // 接收通知：
        let notificationName = Notification.Name(rawValue: "DownloadImageNotification")
        _ = NotificationCenter.default.rx
            .notification(notificationName)
            .takeUntil(self.rx.deallocated) //页面销毁自动移除通知监听
            .subscribe(onNext: { notification in
                //获取通知数据
                let userInfo = notification.userInfo as! [String: AnyObject]
                let value1 = userInfo["value1"] as! String
                let value2 = userInfo["value2"] as! Int
                print("\(name) 获取到通知，用户数据是［\(value1),\(value2)］")
                //等待3秒
                sleep(3)
                print("\(name) 执行完毕")
            })
    }
}

class NotificationCenterTestViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!
    
    let disposeBag = DisposeBag()
    
    let observers = [MyObserver(name: "观察器1"),MyObserver(name: "观察器2")]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "56.通知NotificationCenter的使用"
        view.backgroundColor = .white
        
        textField.borderStyle = UITextBorderStyle.roundedRect
        textField.returnKeyType = .done
        
        test()
        test2()
        Observable.of(1)
            .delaySubscription(1, scheduler: MainScheduler.instance) //延迟3秒才开始订阅
            .subscribe {[weak self] (_) in
                self?.test3()
        }
    }
    
    func test() {
        // 监听应用进入后台通知
        _ = NotificationCenter.default.rx
            .notification(NSNotification.Name.UIApplicationDidEnterBackground)
            .takeUntil(self.rx.deallocated) //页面销毁自动移除通知监听
            .subscribe(onNext: { _ in
                print("程序进入到后台了")
            })
    }
    
    func test2() {
        //点击键盘上的完成按钮后，收起键盘
        textField.rx.controlEvent(.editingDidEndOnExit)
            .subscribe(onNext: {[weak self]  _ in
                //收起键盘
                self?.textField.resignFirstResponder()
            })
            .disposed(by: disposeBag)
         
        //监听键盘弹出通知
        _ = NotificationCenter.default.rx
            .notification(NSNotification.Name.UIKeyboardWillShow)
            .takeUntil(self.rx.deallocated) //页面销毁自动移除通知监听
            .subscribe(onNext: { _ in
                print("键盘出现了")
            })
         
        //监听键盘隐藏通知
        _ = NotificationCenter.default.rx
            .notification(NSNotification.Name.UIKeyboardWillHide)
            .takeUntil(self.rx.deallocated) //页面销毁自动移除通知监听
            .subscribe(onNext: { _ in
                print("键盘消失了")
            })
    }

    func test3() {
        print("发送通知")
        let notificationName = Notification.Name(rawValue: "DownloadImageNotification")
        NotificationCenter.default.post(name: notificationName, object: self,
                                        userInfo: ["value1":"hangge.com", "value2" : 12345])
        print("通知完毕")
    }
}
